More articles »

blog2.utf8

Data Project Example 2: Visualizing Spatial Data

Project Description

The California Department of Fish and Game (DFG) oil spill incident tracking database system provides quantified statistical data on oil spill incident response via the Office of Spill Prevention and Response (OSPR). An incident is defined as any discharge or threatened discharge of petroleum or other deleterious material into the waters of the state. In this simple data project, 2008 California oil spill spatial data is explored and visualized via an interactive tmap and a static chloropleth map. The maps show the exact locations of all recorded 2008 oil spill events in California and those occurring inland by county. To see more information about the data, click here. To access an .html file with only project code, click here.

knitr::opts_chunk$set(echo = TRUE, 
                      warning = FALSE, 
                      message = FALSE)

# Attaching relevant packages:

library(tidyverse) # Wickham et al. 2019
library(here) # Müller 2020
library(janitor) # Firke 2021
library(sf) # Pebesma 2018
library(tmap) # Tennekes 2018
library(gstat) # Pebesma 2004, Gräler et al. 2016
library(stars) # Pebesma 2021

I. Locations of 2008 Oil Spill Events in CA

# Reading in the "CA DFW Oil Spill Incident Tracking" data:

ca_oil_spills <- read_sf(here("ca_oil_spills"), 
                         layer = "ds394") %>% 
  clean_names()

# Creating an exploratory interactive 'tmap':

tmap_mode("view")

tm_shape(ca_oil_spills) + 
  tm_dots("red")

Figure 2.1. An interactive map of California detailing the exact locations of all recorded 2008 oil spill events in and off the coast of the state (Source: Cal EMA & DFG-OSPR 2009).

II.

# Reading in the CA county shapefile data:

ca_counties <- read_sf(here("ca_counties"), 
                       layer = "CA_Counties_TIGER2016") %>% 
  clean_names() %>% 
  select(name) %>% 
  rename(county_name = name)

# Wrangling the data to specify the total count of inland oil spill events by county:

ca_inland_spills <- ca_oil_spills %>% 
  filter(inlandmari == "Inland") %>% 
  count(localecoun) %>% 
  rename(count = n)

# Matching the CRS of the two data frames and then joining:

ca_inland_spills <- st_transform(ca_inland_spills, 
                                 3857)

ca_spill_counts <- ca_counties %>% 
  st_join(ca_inland_spills)
  
# Creating a finalized static chloropleth map:

ggplot(data = ca_spill_counts) +
  geom_sf(aes(fill = count), 
          color = "darkred", 
          size = 0.1) +
  theme_void() + 
  scale_fill_gradientn(colors = c("yellow", 
                                  "red", 
                                  "darkred"), 
                       name = "Total Number\nof Oil Spills") + 
  labs(title = "2008 Oil Spill Events in CA Occurring Inland by County") + 
  theme(plot.title = element_text(face = "bold", 
                                  hjust = 0.5, 
                                  vjust = 2.5))

Figure 2.2. A static chloropleth map of California detailing the total counts of all recorded 2008 oil spill events in the state, occurring inland by county (Source: Cal EMA & DFG-OSPR 2009).

Citations

Cal EMA & DFG-OSPR. (2009). Oil spill incident tracking (Edition 2008) [Raw data]. California Department of Fish and Game, Office of Spill Prevention and Response, Sacramento, USA.

Firke, S. (2021). janitor: Simple tools for examining and cleaning dirty data [R package].

Gräler, B., Pebesma, E., & Heuvelink, G. (2016). Spatio-temporal interpolation using gstat [R package]. The R Journal, 8(1), 204-218.

Müller, K. (2020). here: A simpler way to find your files [R package].

Pebesma, E. (2004). Multivariable geostatistics in S: The gstat package [R package]. Computers & Geosciences, 30, 683-691.

Pebesma, E. (2018). Simple features for R: Standardized support for spatial vector data [R package]. The R Journal, 10(1), 439-446.

Pebesma, E. (2021). stars: Spatiotemporal arrays, raster and vector data cubes [R package].

RStudio Team. (2021). RStudio: Integrated development environment for R (Version 4.0.3) [Computer software]. RStudio PBC, Boston, USA.

Tennekes, M. (2018). tmap: Thematic maps in R [R package]. Journal of Statistical Software, 84(6), 1-39.

Wickham et al. (2019). Welcome to the tidyverse [R package]. Journal of Open Source Software, 4(43), 1686.

webpage contains ip @ 2021 Carlos Simms